<?php
/**
 * Post.php
 *
 * @category    Madoqua
 * @package     Module
 * @subpackage  BlogAdmin
 */

/** base class*/
require_once 'Madoqua/Form.php';

/** madoqua html filter */
require_once 'Madoqua/Filter/Html.php';

/** html entities filter */
require_once 'Zend/Filter/HtmlEntities.php';

/**
 * BlogAdmin_Post_Post
 *
 * @category    Madoqua
 * @package     Module
 * @subpackage  BlogAdmin
 */
class BlogAdmin_Post_Post extends Madoqua_Form
{
    /**
     * no html filter
     *
     * @var Zend_Filter_HtmlEntities
     */
    protected $_noHtml;

    /**
     * constructor
     */
    public function __construct() {
        parent::__construct();

        $this->_noHtml = new Zend_Filter_HtmlEntities(ENT_COMPAT, 'UTF-8');
        //htmlentities filter for non-html fields

        $mainForm = $this->getMainForm();
        $this->addSubForm($mainForm, 'main');

        $optionsForm = $this->getOptionsForm();
        $this->addSubForm($optionsForm, 'options');
        //options subform

        $categoriesForm = $this->getCategoriesForm();
        $this->addSubForm($categoriesForm, 'categories');

        $this->addElement('submit', 'submit', array(
            'label' => 'Submit'
            ));
            //submit

    }

    /**
     * populate from post 
     *
     * @param Blog_Post_Row $post
     */
    public function populateFromPost($post) {
        $data = $post->toArray();
        $this->populate($data);
        //populate "regular" data
        
        $categories = $post->categories;
        $ids = array();
        foreach($categories as $category) {
            $ids[] = $category->id;
        }
        $this->categories->populate(array('category' => $ids));
        //populate the categories
    }

    /**
     * get "main" form for post
     *
     * @return Zend_Form_SubForm
     */
    protected function getMainForm() {
        $form = new Zend_Form_SubForm();
        //the subform

        $form->addElement('text', 'title', array(
            'label' => 'Title',
            'required' => true,
            'validators' => array(
        array('stringLength', false, array(3, 255))
        ),
            'filters' => array(
        $this->_noHtml
        )
        ));
        //title

        $form->addElement('textarea', 'content', array(
            'label' => 'Content',
            'required' => true,
            'validators' => array(
        ),
            'filters' => array(
        new Madoqua_Filter_Html()
        )
        ));
        //title

        return $form;
    }

    /**
     * get post options subform
     *
     * @return Zend_Form_SubForm
     */
    protected function getOptionsForm() {

        $form = new Zend_Form_SubForm();
        //the subform

        $form->addElement('textarea', 'intro', array(
            'label' => 'Introduction',
            'required' => false,
            'validators' => array(
        ),
            'filters' => array(
        $this->_noHtml
        )
        ));
        //title

        $form->addElement('text', 'slug', array(
            'label' => 'Slug',
            'required' => false,
            'validators' => array(
        array('stringLength', false, array(0, 255))
        ),
            'filters' => array(
        $this->_noHtml
        )
        ));
        //slug

        return $form;
        //FIXME add slug validator (unique)
    }

    /**
     * get a the category subform
     *
     * @return Zend_Form_SubForm
     */
    protected function getCategoriesForm() {
        $form = new Zend_Form_SubForm();

        $loader = Zend_Controller_Action_HelperBroker::getStaticHelper('modelLoader');
        $loader->load('Blog_Category');
        $categoryModel = new Blog_Category();
        //category model

        $categories = $categoryModel->fetchAll();
        //all categories

        $options = array();
        foreach($categories as $category) {
            $options[$category->id] = $category->name;
        }
        //categories in key => name format

        $form->addElement('multiCheckbox', 'category', array(
            'multiOptions' => $options
        ));
        //add multi check box

        return $form;
    }
}